import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import { useMount, useUnmount, useUpdateEffect } from "ahooks";
import { useRef } from "react";
var isBrowser = !!(typeof window !== "undefined" && window.document && window.document.createElement);
function isDocumentVisible() {
    if (isBrowser) {
        return document.visibilityState !== "hidden";
    }
    return true;
}
var listeners = [];
function subscribe(listener) {
    listeners.push(listener);
    return function unsubscribe() {
        var index = listeners.indexOf(listener);
        listeners.splice(index, 1);
    };
}
if (isBrowser) {
    var revalidate = function() {
        if (!isDocumentVisible()) return;
        for(var i = 0; i < listeners.length; i++){
            var listener = listeners[i];
            listener();
        }
    };
    window.addEventListener("visibilitychange", revalidate, false);
}
/**
 *
 * @param fetchInstance
 * @param param1
 * @returns {cancel,run}
 */ var usePolling = function(fetchInstance, param) {
    var _param_pollingInterval = param./** 轮询间隔 */ pollingInterval, pollingInterval = _param_pollingInterval === void 0 ? 1000 : _param_pollingInterval, _param_pollingWhenHidden = param./** 再页面不可见时关闭轮询 */ pollingWhenHidden, pollingWhenHidden = _param_pollingWhenHidden === void 0 ? true : _param_pollingWhenHidden, _param_pollingErrorRetryCount = param./** 重试 */ pollingErrorRetryCount, pollingErrorRetryCount = _param_pollingErrorRetryCount === void 0 ? -1 : _param_pollingErrorRetryCount, _param_manual = param./** 为true 需要手动触发轮询 */ manual, manual = _param_manual === void 0 ? false : _param_manual;
    var timerRef = useRef();
    var unsubscribeRef = useRef();
    var countRef = useRef(0);
    var stopPolling = function() {
        var _unsubscribeRef_current;
        if (timerRef.current) {
            clearTimeout(timerRef.current);
        }
        (_unsubscribeRef_current = unsubscribeRef.current) === null || _unsubscribeRef_current === void 0 ? void 0 : _unsubscribeRef_current.call(unsubscribeRef);
    };
    useUpdateEffect(function() {
        if (!pollingInterval) {
            stopPolling();
        }
    }, [
        pollingInterval
    ]);
    if (!pollingInterval) {
        return {};
    }
    var start = function() {
        var _ref = _async_to_generator(function() {
            var res, error;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        stopPolling();
                        _state.label = 1;
                    case 1:
                        _state.trys.push([
                            1,
                            3,
                            ,
                            4
                        ]);
                        return [
                            4,
                            fetchInstance()
                        ];
                    case 2:
                        res = _state.sent();
                        if (res && res.code && res.code !== 200) {
                            throw res;
                        }
                        countRef.current = 0;
                        if (pollingErrorRetryCount === -1 || pollingErrorRetryCount !== -1 && countRef.current <= pollingErrorRetryCount) {
                            timerRef.current = setTimeout(function() {
                                if (!pollingWhenHidden && !isDocumentVisible()) {
                                    unsubscribeRef.current = subscribe(function() {
                                        start();
                                    });
                                } else {
                                    start();
                                }
                            }, pollingInterval);
                        } else {
                            countRef.current = 0;
                        }
                        return [
                            3,
                            4
                        ];
                    case 3:
                        error = _state.sent();
                        console.log("error", error);
                        countRef.current += 1;
                        start();
                        return [
                            3,
                            4
                        ];
                    case 4:
                        return [
                            2
                        ];
                }
            });
        });
        return function start() {
            return _ref.apply(this, arguments);
        };
    }();
    /** 发起请求 */ useMount(function() {
        if (!manual) {
            start();
        }
    });
    useUnmount(function() {
        stopPolling();
    });
    return {
        cancel: function() {
            stopPolling();
        },
        run: function() {
            start();
        }
    };
};
/** 轮询 */ export default usePolling;

 //# sourceMappingURL=usePolling.js.map